package me.baicai.weixin.provider.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import me.baicai.weixin.application.Result;
import me.baicai.weixin.provider.service.OrderService;
import me.baicai.weixin.utils.WeiXinUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;




@RestController
public class OrderController
{

    @Autowired
    private OrderService orderService;

    @ApiOperation("产品套餐获取")
    @PostMapping("/order/menu")
    public Result orderMenu(
        @RequestHeader("openid") String openid, @RequestParam("sn") String sn,  @RequestParam(value = "city",defaultValue = "none",required = false) String city ,@RequestParam(value = "region", defaultValue = "none",required = false) String region)
    {
        return orderService.orderMenu(openid, sn, city, region);
    }

    @ApiOperation("七牛云Token获取")
    @RequestMapping("/order/token")
    public Result qiNiuToken()
    {
        return orderService.getToken();
    }

    @ApiOperation("故障上报")
    @PostMapping("/order/fault")
    public Result fault(@RequestHeader("openid") String openid,
                        @RequestParam("sn") String sn,
                        @RequestParam("tel") String tel,
                        @RequestParam("message") String message,
                        @RequestParam("images") String images,
                        @RequestParam("info") String info)
    {
        return orderService.fault(openid, sn, tel, message, images,info);
    }

    @ApiOperation("提前结束该订单")
    @PostMapping("/order/closeOrder")
    public Result closeOrder(@RequestHeader("openid") String openid,
                             @RequestParam("id") String id)
    {
        return orderService.closeOrder(openid, id);
    }

    @ApiOperation("充电码重置")
    @PostMapping("/order/resetPasswd")
    public Result resetPasswd(@RequestParam("sn") String sn,
                              @RequestParam("id") String id)
    {
        return orderService.resetPasswd(id, sn);
    }

    @ApiOperation("提现记录")
    @PostMapping("/order/withdrawalsRecord")
    public Result withdrawalsRecord(@RequestHeader("openid") String openid,
                                    @RequestParam("beginDate") String beginDate,
                                    @RequestParam("endDate") String endDate)
    {

        return orderService.withdrawalsRecord(openid, beginDate, endDate);
    }

    @ApiOperation("账户余额获取")
    @PostMapping("/order/balance")
    public Result balance(@RequestHeader("openid") String openid)
    {
        return orderService.balance(openid);
    }

    @ApiOperation("余额下单")
    @PostMapping("/order/createOrder")
    public Result createOrder(@RequestHeader("openid") String openid,
                              @RequestParam("id") Integer id,
                              @RequestParam("sn") String sn)
    {
        return orderService.createOrder(openid, sn, id);
    }

    @ApiOperation("微信支付统一下单")
    @PostMapping("/order/weixinPay")
    public Result weixinPay(@RequestHeader("openid") String openid,
                              @RequestParam("id") Integer id,
                              @RequestParam("sn") String sn)
    {
        return orderService.weixinPay(openid, sn, id);
    }

    @ApiOperation("微信支付统一下单回调")
    @RequestMapping(value = "/order/weixinCallback", produces="text/html;charset=utf-8")
    public void weixinCallback(HttpServletRequest request, HttpServletResponse response)
        throws IOException
    {
        BufferedReader br = new BufferedReader(
            new InputStreamReader((ServletInputStream)request.getInputStream()));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null)
        {
            sb.append(line);
        }
        br.close();
        //sb为微信返回的xml
        String notityXml = sb.toString();
        String resXml = "";

        JSONObject jsonObject = WeiXinUtils.convertXmlIntoJSONObject(notityXml);
        if ("SUCCESS".equals(jsonObject.getString("return_code")))
        {
            resXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";

            BufferedOutputStream out = new BufferedOutputStream(
                response.getOutputStream());
            out.write(resXml.getBytes());
            out.flush();
            out.close();
        }
        orderService.weixinCallback(jsonObject);

    }


    @ApiOperation("支付失败关闭订单")
    @PostMapping("/order/closeWeixinOrder")
    public Result closeWeixinOrder(@RequestParam("id") String id)
    {
        return orderService.closeWeixinOrder(id);
    }


    @ApiOperation("微信充值")
    @PostMapping("/order/reCharge")
    public Result reCharge(@RequestHeader("openid") String openid,
                           @RequestParam("amount") Integer amount)
    {
        return orderService.reCharge(openid,amount);
    }

    @ApiOperation("微信充值成功回调")
    @PostMapping("/order/reChargeCallBack")
    public Result reChargeCallBack(@RequestHeader("openid") String openid,
                                    @RequestParam("id") Integer id,
                                   @RequestParam("status") String status)
    {
        return orderService.reChargeCallBack(openid,id,status);
    }

}
